/*
 MYouTubePlayer
 
 Copyright (c) 2007-2008 Mary Jane Soft - Marlon J. Manrique
 	
 http://mjs.darkgreenmedia.com
 http://marlonj.darkgreenmedia.com
 
 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 
 $Id: MYouTubePlayer.pde 375 2008-02-28 14:39:27Z marlonj $
 	
 */

import mjs.processing.mobile.mvideo.*;
import processing.xml.*;

/**
 * MYouTubePlayer
 *
 * MYouTubePlayer is an application to search and watch videos from site :
 * http://m.youtube.com using mobile devices.
 *
 * This application is written in Mobile Processing and use the Net, XML and 
 * MVideo libraries to retrieve, analyze and play videos.
 *
 * The user can give some text information to perform the search using the
 * softkey.
 *
 * The application retrieves the data directly from the http page locate in the
 * url http://m.youtube.com, to access this page a cookie must be get from the 
 * page http://m.youtube.com/?warned=yes and send every time that a search is 
 * performed using the url http://m.youtube.com/results?q=searchText, then the 
 * result xhtml must be parse to seek the current list of videos, each video 
 * entry is marked with a vEntry with the title, url, thumbnail, runtime for 
 * each video.
 *
 * After parsing the result page, a ImageBrowser is created with the list of 
 * the thumbnails videos and is load using the Net library. Through the arrow 
 * keys the user can browse the result and with the FIRE key can load and play 
 * the video.
 *
 * The current version of the NET and XML libraries lack of some features 
 * require to perform the data acquisition, the support of cookies and the 
 * support of entities in the XML. This features are added through the MNet and
 * MXml classes that implements the missing features.
 *
 * Some other class are provide to create a better object oriented design to 
 * the application.
 *
 * @author Marlon J. Manrique
 * @version 0.1 (2007,07,02)
 *
 * $Id: MYouTubePlayer.pde 375 2008-02-28 14:39:27Z marlonj $
 */

// Components
ImageBrowser imageBrowser;
VideoLoader videoLoader;
VideoPlayer videoPlayer;

// Main elements
PFont font;
String title = "MYouTubePlayer";
String message = "Ready";
String foot = "";

// If a key event is consumed by a component
boolean keyEventConsumed;

/**
 * Start the application.
 *
 * Create objects, load font
 */
void setup()
{ 
  // Font settings
  font = loadFont();
  textAlign(CENTER);

  // Create the components
  imageBrowser = new ImageBrowser(this);
  videoLoader = new VideoLoader(this);
  videoPlayer = new VideoPlayer(this);
}

/**
 * Draw all components and basic elements
 */
void draw()
{
  // Draw components 
  videoLoader.draw();   
  imageBrowser.draw();
  videoPlayer.draw();

  // Draw elements
  textFont(font);
  fill(0xFFFFFF);
  text(title,0,-60);
  text(message,0,55+font.height);  
  text(foot,0,55+font.height*2);
}

/**
 * Check the sofkey
 */
void softkeyPressed(String label)
{
  // Only videoloader have softkeys
  videoLoader.softkeyPressed(label);
}

/**
 * Check user interaction
 */
void keyPressed()
{
  keyEventConsumed = false;
  imageBrowser.keyPressed();  
  videoPlayer.keyPressed();
  videoLoader.keyPressed();
}

/**
 * Listen the events
 */
void libraryEvent(Object library, int event, Object data)
{
  imageBrowser.libraryEvent(library,event,data);
  videoLoader.libraryEvent(library,event,data);  
  videoPlayer.libraryEvent(library,event,data);   
}
